package main.swing;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import main.bean.Email;
import main.inte.Pop4recv;
import main.param.GlobalParam;

public class ViewEmail extends JFrame {
	JList jList;
	List<Email> list;
	JTextArea textArea ;
	public ViewEmail() {
		setSize(new Dimension(550, 350));
		BorderLayout borderLayout = (BorderLayout) getContentPane().getLayout();
		borderLayout.setHgap(10);

		JScrollPane scrollPane = new JScrollPane();
		getContentPane().add(scrollPane, BorderLayout.EAST);

		textArea = new JTextArea();
		textArea.setColumns(40);
		scrollPane.setViewportView(textArea);

		JScrollPane scrollPane_1 = new JScrollPane();
		getContentPane().add(scrollPane_1, BorderLayout.WEST);

		jList = new JList();
		jList.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				// System.out.println(jList.getSelectedIndex());
				showMainText(jList.getSelectedIndex());
			}
		});

		scrollPane_1.setViewportView(jList);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu mnNewMenu = new JMenu("File");
		menuBar.add(mnNewMenu);

		JMenuItem mntmNewEmial = new JMenuItem("New Email");
		mntmNewEmial.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SendEmail sendEmail = new SendEmail();
				
			}
		});
		mnNewMenu.add(mntmNewEmial);

		JMenu mnNewMenu_1 = new JMenu("Sys");
		
		menuBar.add(mnNewMenu_1);

		JMenuItem mntmUserAccount = new JMenuItem("User Account");
		mntmUserAccount.setEnabled(false);
		mnNewMenu_1.add(mntmUserAccount);

		JMenuItem mntmExit = new JMenuItem("Exit");
		mntmExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);

			}
		});
		mnNewMenu_1.add(mntmExit);
		if(!GlobalParam.Ready){
			JOptionPane.showMessageDialog(null,"please check out you properties file");
		}
	}

	public void showMainText(int index) {
		Email e = list.get(index);
		String text = e.getSubject() + "\n" + e.getFrom() + "\n"
				+ e.getSentDate() + "\n" +
				e.isContainerAtt() + "\n" + e.getText() + "\n";
		textArea.setText(text);
	}

	public void Mail(){
	
		setVisible(true);
		Pop4recv pop = new Pop4recv();
		list = pop.recp();
		List strs = new ArrayList();
		for (Email e : list) {
			strs.add(e.getSubject().length() > 5 ? e.getSubject().substring(0,
					4) : e.getSubject());
		}
		jList.setListData(strs.toArray());
	}
	public static void main(String[] args) {
		ViewEmail v = new ViewEmail();
		v.Mail();
	}
}
